package com.jinbo.sxzq;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
import java.util.zip.Inflater;

/*************棋盘控制***************/
public class GameControl {
    private int[][] Board_location;//保存所有棋子位置的数组
    private int[][] Board_owner;//棋子所有权
    private int[] Board_deploy;//部署区域的棋子类型集合
    public List<Chess> chess_list;//棋盘中棋子对象的集合
    public Chess action_chess;//当前正在行动的棋子

    int max_deploy;//最大棋子部署数
    int num_deploy;//我方棋子棋子部署数
    int sum_deploy;//总的棋子部署数
    int round_num;//当前回合数
    int death_num_o;//我方棋子死亡数
    int death_num_e;//敌方棋子死亡数
    int chess_index;//创建棋子时的编号
    public int gamestate;//游戏状态
    GameControlThread gameControlThread;//运算游戏进程的线程
    GameControl(int gameLevel){
        max_deploy = ProjectConstants.BoardAttributes.MAX_DEPLOY;
        num_deploy = 0;
        sum_deploy = 0;
        chess_index = 0;
        death_num_e = 0;
        death_num_o = 0;
        gamestate = ProjectConstants.GameState.Game_DEPLOY;
        LevelMessage levelMessage = new LevelMessage(gameLevel);
        Board_location = levelMessage.getLevelmessage();
        Board_owner = levelMessage.getLevelowner();
        chess_list = new ArrayList<>();
        gameControlThread = new GameControlThread(this);
    }

    public void StarGame(){//开启游戏计算线程的方法
        gamestate = ProjectConstants.GameState.GAME_PLAY;
        sum_deploy = initChess();
        action_chess = chess_list.get(0);
        gameControlThread.setFlag(true);
        gameControlThread.start();//开启计算线程
        Log.e("游戏控制","总棋子数" + sum_deploy + "我方棋子数" + num_deploy);
        Log.e("游戏控制","队列长度" + chess_list.size());
        Log.e("游戏控制","游戏开始");
    }

    public void EndGame(int endstate){//结束游戏计算线程的方法
        switch (endstate) {
            case 0://玩家A胜出
                Log.e("游戏控制","游戏结束A胜出");
                break;
            case 1://玩家B/电脑胜出
                Log.e("游戏控制","游戏结束B胜出");
                break;
            case 2://回合超时
                Log.e("游戏控制","游戏结束超时");
                break;
        }
        boolean retry = true;
        gameControlThread.setFlag(false);//关闭计算线程
        while (retry) {
            try {
                gameControlThread.join();//阻塞线程
                retry = false;
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void PlayGame(){//控制游戏执行一次操作的方法（操作索引选中的棋子；控制回合的进程；）
        boolean flag = false;//标记队列中有可行动的棋子
        if(action_chess.mobility > 0){//当前棋子可以行动
            flag = true;
            action_chess.Action();
        }else{
            for(int i = 0 ; i < chess_list.size() ; i++){
                if(chess_list.get(i).index == action_chess.index  && i < chess_list.size()-1){
                        flag = true;
                        action_chess = chess_list.get(i+1);
                        action_chess.Action();
                }
            }
        }
        /*
        if(chess_list.get(list_index).mobility > 0){
            flag = true;
            chess_list.get(list_index).Action();
        }else{//不可行动则找寻下一个可以行动的棋子
            for(int i = list_index+1 ; i < chess_list.size() ; i++){
                if(chess_list.get(i).mobility > 0){//队列中顺位第一个可行动棋子
                    chess_list.get(i).Action();
                    flag = true;
                    list_index = i;
                    break;
                }
            }
        }
         */
        if(flag == false){//此时无可行动的棋子
            RoundGame();
            action_chess = chess_list.get(0);
        }
        //判断是否游戏结束
        if(sum_deploy-num_deploy == 0){
            EndGame(0);
            return;
        }
        if(num_deploy == 0){
            EndGame(1);
            return;
        }
        if(round_num > 20) {
            EndGame(2);
            return;
        }
    }

    //初始化下一回合的方法
    public void RoundGame(){
        for(int i = 0; i < chess_list.size() ; i++){
            if(chess_list.get(i).state == ProjectConstants.ChessState.SURVIVE){
                chess_list.get(i).RoundNext();
            }
        }
        Log.e("游戏控制","当前回合" + round_num);
        round_num++;
    }

    public int[][] getBoard(){
        return Board_location != null ? Board_location : null;
    }

    public void setBoard(int r , int l , int type){Board_location[r][l] = type;}

    public void deployChess(int r , int l , int number,int type , int owner){//在棋盘中部署一个棋子的方法
        chess_index++;
        switch (type){
            case ProjectConstants.ChessType.SHU:
                Log.e("棋子部署","部署棋子-鼠 " + owner);
                chess_list.add(number , new Chess_Shu(type , chess_index , new int[]{r,l} , owner , this));
                break;
            case ProjectConstants.ChessType.NUI:
                Log.e("棋子部署","部署棋子-牛 " + owner);
                chess_list.add(number , new Chess_Nui(type , chess_index , new int[]{r,l} , owner , this));
                break;
            case ProjectConstants.ChessType.HU:
                Log.e("棋子部署","部署棋子-虎 " + owner);
                chess_list.add(number , new Chess_Hu(type , chess_index , new int[]{r,l} , owner , this));
                break;
            case ProjectConstants.ChessType.TU:
                Log.e("棋子部署","部署棋子-兔 " + owner);
                chess_list.add(number , new Chess_Tu(type , chess_index , new int[]{r,l} , owner , this));
                break;
            case ProjectConstants.ChessType.LONG:
                Log.e("棋子部署","部署棋子-龙 " + owner);
                chess_list.add(number , new Chess_Long(type , chess_index , new int[]{r,l} , owner , this));
                break;
            case ProjectConstants.ChessType.SHE:
                Log.e("棋子部署","部署棋子-蛇 " + owner);
                chess_list.add(number , new Chess_She(type , chess_index , new int[]{r,l} , owner , this));
                break;
            case ProjectConstants.ChessType.MA:
                Log.e("棋子部署","部署棋子-马 " + owner);
                chess_list.add(number , new Chess_Ma(type , chess_index , new int[]{r,l} , owner , this));
                break;
            case ProjectConstants.ChessType.YANG:
                Log.e("棋子部署","部署棋子-羊 " + owner);
                chess_list.add(number , new Chess_Yang(type , chess_index , new int[]{r,l} , owner , this));
                break;
            case ProjectConstants.ChessType.HOU:
                Log.e("棋子部署","部署棋子-猴 " + owner);
                chess_list.add(number , new Chess_Hou(type , chess_index , new int[]{r,l} , owner , this));
                break;
            case ProjectConstants.ChessType.JI:
                Log.e("棋子部署","部署棋子-鸡 " + owner);
                chess_list.add(number , new Chess_Ji(type , chess_index , new int[]{r,l} , owner , this));
                break;
            case ProjectConstants.ChessType.GOU:
                Log.e("棋子部署","部署棋子-狗 " + owner);
                chess_list.add(number , new Chess_Gou(type , chess_index , new int[]{r,l} , owner , this));
                break;
            case ProjectConstants.ChessType.ZHU:
                Log.e("棋子部署","部署棋子-猪 " + owner);
                chess_list.add(number , new Chess_Zhu(type , chess_index , new int[]{r,l} , owner , this));
                break;
                default:
                  Log.e("棋子部署","未知棋子类型");
          }
    }

    public int initChess(){//初始化棋子队列的方法
        int index = 0;//棋子编号（第几枚棋子）
        for(int i = 0 ; i < ProjectConstants.BoardAttributes.WIDTH ; i++){
            for(int j = 0 ; j < ProjectConstants.BoardAttributes.LENGTH ; j++){
                if(Board_location[i][j] != 0){
                    int number = chess_list.size();//有序插入的位置
                    for(int k = 0 ; k < chess_list.size() ; k++){
                        if(Board_location[i][j] < chess_list.get(k).type){
                            number = k;
                            break;
                        }
                    }
                    index++;
                    deployChess(i,j,number,Board_location[i][j],Board_owner[i][j]);
                }
            }
        }
        Log.e("游戏控制","初始化部署队列");
        return index;
    }

    public int getBoardChess(int r , int l){
        return Board_location[r][l];
    }

    public int getBoardOwner(int r , int l){
        return Board_owner[r][l];
    }

    public void setBoardOwner(int r , int l , int owner){
        Board_owner[r][l] = owner;
    }
}
